package com.fgchen.study.velocity.entity;

import org.apache.velocity.util.StringUtils;

public class Column {

    private String name;

    private String type;

    private String comment;

    public Column() {}

    public String getFieldName() {
        if (this.name.contains("_")) {
            String[] parts = StringUtils.split(this.name, "_");
            StringBuffer sb = new StringBuffer();
            for (String part : parts) {
                sb.append(StringUtils.firstLetterCaps(part));
            }
            return org.apache.commons.lang.StringUtils.uncapitalize(sb.toString());
        } else {
            return this.name.toLowerCase();
        }
    }

    public String getFieldType() {
        if (org.apache.commons.lang.StringUtils.isEmpty(this.type)) {
            return "String";
        }

        String ctype = this.type.toUpperCase();

        System.out.println("name: " + name + ", type: " + type);

        if ("BIGINT".equals(ctype)) {
            return "Long";
        }

        if ("SMALLINT".equals(ctype)
                || "INTEGER".equals(ctype)
                || "TINYINT".equals(ctype)) {
            return "Integer";
        }

        if ("FLOAT".equals(ctype)
                || "DOUBLE".equals(ctype)
                || "DECIMAL".equals(ctype)) {
            return "Double";
        }

        if ("DATE".equals(ctype)
                || "TIME".equals(ctype)
                || "DATETIME".equals(ctype)
                || "TIMESTAMP".equals(ctype)) {
            return "Date";
        }
        return "String";
    }

    public Column(String name, String type, String comment) {
        this.name = name;
        this.type = type;
        this.comment = comment;
    }

    public String getName() {
        return name;
    }

    public Column setName(String name) {
        this.name = name;
        return this;
    }

    public String getType() {
        return type;
    }

    public Column setType(String type) {
        this.type = type;
        return this;
    }

    public String getComment() {
        return comment;
    }

    public Column setComment(String comment) {
        this.comment = comment;
        return this;
    }
}
